import store from 'store'
import axios from 'axios'

import { push } from 'react-router-redux'
import { inputUserInfo, getPreselect, getLockHouse } from 'model/user'
import { inputBuildingInfo, getBuilding } from 'model/building'
import { getHouse } from 'model/house'

export default async function login (Store) {
  const user = store.get('user')
  const building = store.get('building')
  console.log(user, building)

  if (
    !user ||
    !user.phone ||
    !building ||
    !building.propertyId ||
    !building.housePreiodId
  ) {
    return
  }

  await Store.dispatch(inputUserInfo(user))
  await Store.dispatch(inputBuildingInfo(building))
  // axios.defaults.headers.common['myToken'] = 'eyJhbGciOiJIUzUxMiJ9.eyJleHAiOjE1NTM0NDE0NjQsInVzZXIiOnsiaWQiOjE1NTIzOTE1MzI3ODc3OSwibmFtZSI6Iua1i-ivlSIsInBob25lIjoiMTM2MzU0ODYwODQiLCJwcm9wZXJ0eUlkIjoxOTAyMjcwOTU4MzU3NjMyMzMsInBlcmlvZElkIjpudWxsfX0.9IlzVW5d27g8BkvNEFq3chQPL3WOH2PmCsSJj72cso8NVLcD3_PA_knKnXKW4u2us9qmQ3yXN9QgKna2sjo3Dg'

  // 跳转页面

  // 只有当前页面不是主页才跳到这个主页去
  // if (Store.getState().router.location.pathname !== '/home') {
  //   Store.dispatch(push('/home'))
  // }

  // 初始化数据
  const {
    phone
  } = user

  const {
    propertyId,
    housePreiodId
  } = building

  Store.dispatch(getBuilding({
    phone,
    propertyId,
    housePreiodId,
  }))

  Store.dispatch(getHouse({
    phone,
    propertyId,
    housePreiodId,
  }))

  Store.dispatch(getPreselect({
    phone,
    propertyId,
    housePreiodId,
  }))

  Store.dispatch(getLockHouse({
    phone,
    propertyId,
    housePreiodId,
  }))
}
